﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Collections.Specialized;
using Meraz.Core.Model;

namespace Meraz.Core.Parsers
{
    public class ThinkProgressParser : RegexBlogParser
    {
        string alias = null;
        string dateString = null;

        public ThinkProgressParser() { this.BlogKey = BlogKeyType.ThinkProgress; }
        
        internal override void CleanUp(PostValues post)
        {
            post.Links.Remove("http://thinkprogress.org/about");
            post.Links.Remove("http://wonkroom.thinkprogress.org/");
        }

        internal string ParseGuest(string body)
        {   
            Regex guestPattern = new Regex(
                @".*<a href=""http://thinkprogress.org/about"">(?<guest>[^<]+)</a>", 
                RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match guestMatch = guestPattern.Match(body);
            if (guestMatch.Success)
                return guestMatch.Groups["guest"].Value.Trim();
            return null;

        }
        internal void ParseByLine(string source)
        {
            Regex bylinePattern = new Regex(
                @".*<span class=""authorname""><a [^>]+>(?<name>[^<]+)</a></span>[\s]*on (?<date>[^<]*)</div>", 
                RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match bylineMatch = bylinePattern.Match(source);
            if (bylineMatch.Success)
            {
                alias = bylineMatch.Groups["name"].Value;
                dateString = bylineMatch.Groups["date"].Value;
            }
        }

        internal override DateTime ParseDate(string source)
        {
            if (dateString == null)
                ParseByLine(source);
            //Jun 30th, 2010 at 7:45 pm 
            Regex dateRegex = new Regex(@"(?<month>\w+) (?<day>\d+)\w*, (?<year>\d\d\d\d) at (?<hour>\d+):(?<min>\d\d) (?<time>(am|pm))", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match dateMatch = dateRegex.Match(dateString.Trim());
            if (dateMatch.Success)
            {
                string year = dateMatch.Groups["year"].Value;
                string month = dateMatch.Groups["month"].Value;
                string day = dateMatch.Groups["day"].Value;
                string hour = dateMatch.Groups["hour"].Value;
                string min = dateMatch.Groups["min"].Value;
                string time = dateMatch.Groups["time"].Value;
                return NewDateTime(year, month, day, hour, min, time);
            }
            return DateTime.MinValue;
        }

        internal override List<string> ParseAliases(string source)
        {
            if (alias == null)
                ParseByLine(source);
            List<string> aliases = new List<string>();
            if (alias.Equals("Guest Blogger"))
                aliases.Add(ParseGuest(source));
            else
                aliases.Add(alias);
            return aliases;
        }

        internal override string ParseBody(string source)
        {
            string marker = @"""entryContent"">";
            int start = source.IndexOf(marker);
            if (start > 0)
            {
                start += marker.Length;
                int end = source.IndexOf(@"<!-- end #entryContent -->", start);
                if (start < end)
                {
                    return source.Substring(start, end - start);
                }
            }
            throw new ApplicationException("Post Body not found");
        }
    }
}
